package org.firefly.fx.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.firefly.fx.constants.ConfigInfo;
import org.firefly.fx.service.common.IWebsocketSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

//TODO delete sooner
@Controller
public class LoginController {
	@Autowired private IWebsocketSessionService wssSerivce;
	/**
	 * login
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/login")
	public String login(HttpServletRequest request , HttpServletResponse response , Model model){
		String userName = request.getParameter("userName");
		
		if(userName==null){
			model.addAttribute("msg", "input name");
			return "login";
		}
		
		request.getSession().setAttribute(ConfigInfo.USER_ID, userName);
		return "redirect:/index";
	}
	
	@RequestMapping(value="/index")
	public String main(HttpServletRequest request){
		if(request.getSession().getAttribute(ConfigInfo.USER_ID)==null){
			return "redirect:/login";
		}
		
		String userId = (String) request.getSession().getAttribute(ConfigInfo.USER_ID);
		wssSerivce.removeSession(userId);
		
		return "index";
	}
	
}